<template>
  <div class="person">
    <h1>情况三:监视【reactive】定义的【对象类型】数据</h1>
    <h2>姓名:{{ person.name }}</h2>
    <h2>年龄:{{ person.age }}</h2>
    <button @click="changeName">修改姓名</button>
    <button @click="changeAge">修改年龄</button>
    <button @click="changePerson">修改整个人</button>
    <hr />
    <h2>{{ obj.a.b.c }}</h2>
    <button @click="test">修改obj.a.b.c</button>
  </div>
</template>

<script lang="ts" setup name="Person">
  import { reactive, watch } from "vue";

  //数据
  let person = reactive({
    name: "张三",
    age: 18,
  });
  let obj = reactive({
    a: {
      b: {
        c: 666,
      },
    },
  });

  //方法
  function changeName() {
    person.name += "~";
  }
  function changeAge() {
    person.age += 1;
  }
  function changePerson() {
    //reactive定义的对象现在修改了person,是将对象的属性的值改了,就是说对象还是那个对象,  而ref是新对象替换了旧对象,对象地址已经发生了改变
    Object.assign(person, { name: "李四", age: 7 });
  }
  function test() {
    obj.a.b.c = 999;
  }

  //监视,  情况三:监视【reactive】定义的【对象类型】数据,且默认是开启深度监视,而且深度监视是不能关闭的,当然了写deep:false也是没用的
  watch(person, (newValue, oldValue) => {
    console.log("person变化了", newValue, oldValue);
  });
  watch(obj, (newValue, oldValue) => {
    console.log("obj变化了", newValue, oldValue);
  });
</script>

<style scoped>
  .person {
    background-color: aqua;
    box-shadow: 0 0 10px;
    border-radius: 10px;
    padding: 20px;
  }

  button {
    margin-left: 5px;
    margin-right: 5px;
  }
</style>
